package com.device.operating.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.Query;

import net.sf.json.JSON;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.device.operating.dao.EmRepository;
import com.device.operating.service.EmService;
import com.device.operating.utils.ToolsStr;

@Service("EmService")
@Transactional(propagation=Propagation.REQUIRED)
public class AEmServiceImpl implements EmService{
	
	@Autowired 
	private EmRepository em;
	
	@Override
	public Object getEntity(String dvbelong, String Idname,String Id) {
		// TODO Auto-generated method stub
//		String newStr=dvbelong.substring(0, 1).toUpperCase()+dvbelong.replaceFirst("\\w","");
//		String sql="select "+dvbelong+" from "+newStr+" "+dvbelong+" where businesskey='"+Id+"'";
//		Query query=em.createQuery(sql);
		String newStr=dvbelong.substring(0, 1).toUpperCase()+dvbelong.replaceFirst("\\w","");
		String sql="select "+dvbelong+" from "+newStr+" "+dvbelong+" where "+Idname+"='"+Id+"'";
		System.out.println(sql);
		Query query=em.createQuery(sql);
		List<Object> list=query.getResultList();
		if(list.size()==0){
			return null;
		}
		return list.get(0);
	}

	@Override
//	@Transactional(propagation=Propagation.NOT_SUPPORTED) 
	public void saveEntity(Object object) {
		// TODO Auto-generated method stub
		em.SaveThisEntity(object);
	}

	@Override
	public void updateEntity(Object obj) {
		// TODO Auto-generated method stub
		em.updateThisEntity(obj);
	}

	@Override
	public List<Object> findEntitylist(String dvbelong, String code,
			String value) {
		// TODO Auto-generated method stub
		String newStr=dvbelong.substring(0, 1).toUpperCase()+dvbelong.replaceFirst("\\w","");
		String sql="";
		if(code.equals("")){
			sql="select "+dvbelong+" from "+newStr+" "+dvbelong;
		}else{
			sql="select "+dvbelong+" from "+newStr+" "+dvbelong+" where "
					+ code+"='"+value+"'";
		}
		System.out.println(sql);
		Query query=em.createQuery(sql);
		List<Object> list=query.getResultList();
		return list;
	}

	@Override
	public void deleteEntity(Object obj) {
		// TODO Auto-generated method stub
		em.deleteThisEntity(obj);
	}

	@Override
	public void deleteEntitylist(List list) {
		// TODO Auto-generated method stub
		for(Object obj:list){
			em.deleteThisEntity(obj);
		}
	}

	@Override
	public List findListAll(String dvbelong, List<String> listcv) {
		// TODO Auto-generated method stub
		String newStr=dvbelong.substring(0, 1).toUpperCase()+dvbelong.replaceFirst("\\w","");
		String sql="select "+dvbelong+" from "+newStr+" "+dvbelong;
		if(listcv.size()!=0){
			sql=sql+" where ";
			int i=0;
			for(String str:listcv){
				i++;
				String code=str.split(",")[0];
				String value=str.split(",")[1];
				sql=sql+code+"='"+value+"'";
				if(i<listcv.size()){
					sql=sql+" and ";
				}
			}
		}
		
		System.out.println(sql);
		Query query=em.createQuery(sql);		
		return query.getResultList();
	}

	@Override
	public Map FindListByParamsAndPages(String fx, String zf, String danxuan, String shuzi, String riqi, String flowkey,
			String type, int fr, int mr) {
		// TODO Auto-generated method stub
		Map map=new HashMap<>();
		String[] ob=null;
		
		String[] sql=ToolsStr.creatsql(fx, zf, danxuan,shuzi,riqi,flowkey,ob,type);
		Query query1=null;
		Query query=null;
		if(type.equals("0")){
			query1=em.createQuery(sql[1]);
			query = em.createQuery(sql[0]);
		}else if(type.equals("1")){
			query1=em.createNativeQuery(sql[1]);
			query =em.createNativeQuery(sql[0]);
		}else if(type.equals("2")){
			query=em.createNativeQuery(sql[0]);
			query1=em.createNativeQuery(sql[1]);
		}
		int count =new Integer(query1.getSingleResult().toString()).intValue();
	    query.setFirstResult(fr*mr);
	    query.setMaxResults(mr);
	    if(type.equals("0")){
		    List<JSON> list=query.getResultList();
		    map.put("rows", list);
		    map.put("total", count);
			return map;
	    }else if(type.equals("1")){
	    	query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		    List list=query.getResultList();
		    map.put("rows", list);
		    map.put("total", count);    
			return map;
	    }else if(type.equals("2")){
	    	query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		    List list=query.getResultList();
		    map.put("rows", list);
		    map.put("total", count);    
			return map;
	    }
	    return null;
	}

	
}
